package com.plian.system.service.fr.financeindicators;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.plian.Tools.DateUtil;
import com.plian.Tools.StringUtil;
import com.plian.Tools.TokenUtil;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.fr.financeindicators.FinanceIndicatorsDao;
import com.plian.system.entity.fr.indicators.FinanceIndicatorsSupplement;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.page.Page;
import com.plian.system.service.fr.report.IReportVersionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* 说明：值标定义表
*/
@Slf4j
@Service
@Transactional
public class FinanceIndicatorsService {

    @Autowired
    FinanceIndicatorsDao financeindicatorsDao;

    @Autowired
    IReportVersionService reportVersionService;

    @Autowired
    IFinanceIndicatorsSupplementService financeIndicatorsSupplementService;

/**
* 保存
* @param pd
* @return
*/
public ObjResult save(PageData pd)  {
    try {
        String id= UuidUtil.get32UUID();
        pd.put("id", id);
        pd.put("createTime",new Date());
        pd.put("updateTime",new Date());
        pd.put("createUser",TokenUtil.getTokenUserId());
        pd.put("updateUser",TokenUtil.getTokenUserId());
        //今年
        pd.put("year", DateUtil.format(DateUtil.now(), DateUtil.PATTERN_YEAR));
        financeindicatorsDao.save(pd);

        FinanceIndicatorsSupplement financeIndicatorsSupplement = getSupplement(pd);
        financeIndicatorsSupplementService.save(financeIndicatorsSupplement);
        ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
        return objResult;

    } catch (Exception e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
        return new ObjResult(CommonCode.ERROR);
    }
}
/**
* 修改
* @param pd
* @return
*/
public ObjResult edit(PageData pd){
    try {
        pd.put("updateTime", new Date());
        pd.put("updateUser", TokenUtil.getTokenUserId());
        //今年
        pd.put("year", DateUtil.format(DateUtil.now(), DateUtil.PATTERN_YEAR));
        financeindicatorsDao.edit(pd);

        financeIndicatorsSupplementService.remove(new QueryWrapper<FinanceIndicatorsSupplement>()
                .eq("finance_indicators_id", pd.getString("id"))
        .eq("year", pd.getString("year")));
        FinanceIndicatorsSupplement financeIndicatorsSupplement = getSupplement(pd);
        financeIndicatorsSupplementService.save(financeIndicatorsSupplement);

        ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
        return objResult;
    } catch (Exception e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
        return new ObjResult(CommonCode.ERROR);
    }
}

private FinanceIndicatorsSupplement getSupplement(PageData pd){
    return FinanceIndicatorsSupplement.builder()
            .financeIndicatorsId(pd.getString("id"))
            .tabName(pd.getString("tabName"))
            .lines(pd.getString("lines"))
            .columns(pd.getString("columns"))
            .fieldName(pd.getString("fieldName"))
            .excelCode(pd.getString("excelCode"))
            .type(pd.getString("type"))
            .year(pd.getString("year"))
            .formula(pd.getString("formula"))
            .descriptor(pd.getString("descriptor"))
            .greatFloating(pd.getString("greatFloating"))
            .dataSource(pd.getString("dataSource"))
            .maxDeviate(pd.getString("maxDeviate"))
            .build();
}
/**
* 查询列表
* @return
*/
public ListResult listPage(PageData pd) {
    ListResult listResult;
    try {
        boolean isPage=true;
        if(pd.containsKey("isPage")){
            isPage= (boolean) pd.get("isPage");
        }
        if (StringUtil.isBlank(pd.getString("year"))){
            pd.put("year", DateUtil.format(DateUtil.now(), DateUtil.PATTERN_YEAR));
        }
        Page page=new Page(isPage,pd);
        List<PageData> list=financeindicatorsDao.listPage(page);
        listResult=new ListResult(CommonCode.SUCCESS);
        listResult.setList(list);
        listResult.setTotal(page.getTotal());
    } catch (Exception e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
        listResult=new ListResult(CommonCode.ERROR);
    }
        return listResult;
    }
    /**
     * 查询预算表类型
     * @return
     */
    public ListResult findBudgetType(PageData pd) {
        ListResult listResult;
        try {
            pd.put("version", reportVersionService.getVersion(pd.getString("year")));
            List<PageData> list=financeindicatorsDao.findbudgettype(pd);
            listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(list);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            listResult=new ListResult(CommonCode.ERROR);
        }
        return listResult;
    }
    /**
     * 查询预算表类型
     * @return
     */
    public ListResult findfilldeviation() {
        ListResult listResult;
        try {

            List<PageData> list=financeindicatorsDao.findfilldeviation();
            listResult=new ListResult(CommonCode.SUCCESS);
            listResult.setList(list);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            listResult=new ListResult(CommonCode.ERROR);
        }
        return listResult;
    }

//    /** TODO DEL
//     * 查询预算表类型
//     * @return
//     */
//    public ListResult findByDataSource(PageData pd) {
//        ListResult listResult;
//        try {
//            pd.put("year", DateUtil.format(DateUtil.now(), DateUtil.PATTERN_YEAR));
//            List<PageData> list=financeindicatorsDao.findByDataSource(pd);
//            listResult=new ListResult(CommonCode.SUCCESS);
//            listResult.setList(list);
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            listResult=new ListResult(CommonCode.ERROR);
//        }
//        return listResult;
//    }
    /**
    * 根据id查询
    * @param pd
    * @return
    */
    public ObjResult findbyid(PageData pd){
        ObjResult objResult;
        String id=pd.getString("id");
    try {
        PageData p=financeindicatorsDao.findbyid(id);
        objResult=new ObjResult(CommonCode.SUCCESS);
        objResult.setPageData(p);
        return objResult;
    } catch (Exception e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
        objResult=new ObjResult(CommonCode.ERROR);
        return objResult;
    }
    }
    /**
     * 根据code查询
     * @param pd
     * @return
     */
    public ObjResult findcountbycode(PageData pd) {
        ObjResult objResult;
        try {
            PageData p = financeindicatorsDao.findcountbycode(pd);
            objResult = new ObjResult(CommonCode.SUCCESS);
            objResult.setPageData(p);
            return objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            objResult = new ObjResult(CommonCode.ERROR);
            return objResult;
        }
    }
    /**
     * 查询预算表类型、1-x月预算风险预警表类型 TODO DEL
     * @return
     */
//    public ListResult findByCodes() {
//        ListResult listResult;
//        try {
//            PageData pd = new PageData();
//            String[] codeList = {"K1","KG1","K19","K38"};
//            pd.put("codeList",codeList);
//            List<PageData> list=financeindicatorsDao.findByCodes(pd);
//            listResult=new ListResult(CommonCode.SUCCESS);
//            listResult.setList(list);
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            listResult=new ListResult(CommonCode.ERROR);
//        }
//        return listResult;
//    }
//    /** TODO DEL
//     * 查询1-12月金额负数表类型（利润、现金流量净额）
//     * @return
//     */
//    public ListResult findDeficitWarningTypeByCodes() {
//        ListResult listResult;
//        try {
//            PageData pd = new PageData();
//            String[] codeList = {"K19","K38"};
//            pd.put("codeList",codeList);
//            List<PageData> list=financeindicatorsDao.findByCodes(pd);
//            listResult=new ListResult(CommonCode.SUCCESS);
//            listResult.setList(list);
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            listResult=new ListResult(CommonCode.ERROR);
//        }
//        return listResult;
//    }
    public ObjResult del_ids(List<String> ids){
        try {
            ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
            financeIndicatorsSupplementService.remove(new QueryWrapper<FinanceIndicatorsSupplement>().in("finance_indicators_id", ids));
            financeindicatorsDao.del_ids(ids);
            return objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            return new ObjResult(CommonCode.ERROR);
        }
    }
}
